<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class admLoginController extends Controller
{
    public function login(Request $request)
    {
        //接收数据username and password
        $a_user = $request->a_name;//user
        $a_pass = $request->a_pass;//pass

        //判断表单是否为空
        if (empty($a_user)) {
            return redirect('/adm/login');
        }
        if (empty($a_pass)) {
            return redirect('/adm/login');
        }
        //查询数据库user数据
        $result = DB::table('admin')->select('id')->where('a_name', $a_user)->get();
        $id = $result['0']->id;//用户ID

        if (!$result) {
            return redirect('/adm/login');
        }
        //查询数据库pass数据
        $list = DB::table('admin')->where('a_pass', $a_pass)->get();
        if (!$list) {
            return redirect('/adm/login');
        }

        //查询成功之后把数据存入
        session(['admin.name' => $a_user]);
        session(['admin.pass' => $a_pass]);
        session(['admin.id' => $id]);

        //存入之后跳转地址
        return redirect('/adm');
    }

    public function out()
    {
        $result = session()->forget('admin');
        if (empty($result)){
            return view('admin.login');
        }
    }
}
